﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Collections.Generic;
using System.Linq;
using Ascend;

namespace AscendViewer
{
	public class ArmatureContentNodeViewModel : ContentNodeViewModel
	{
		#region Lifecycle
		public ArmatureContentNodeViewModel(Armature armature,
			Queue<TransformableObject> axisSceneAliases,
			Queue<TransformableObject> boneSceneAliases, ContentExplorerViewModel container,
			ContentNodeViewModel parent)
			: base(null, axisSceneAliases, boneSceneAliases, container, parent)
		{
			Armature = armature;
			Name = armature.Name;

			LookAtCommand = new InterfaceCommand(() =>
				{
					container.MainWindow.MainScene.LookAt(Armature.Skin);
				});

			if (armature.RootBones != null)
			{
				InitializeChildren(armature.RootBones, axisSceneAliases, boneSceneAliases,
					container);
			}
		}
		#endregion

		#region Public Properties
		public Armature Armature
		{
			get;
			private set;
		}

		private bool _isActive;
		public bool IsActive
		{
			get
			{
				return _isActive;
			}

			set
			{
				if (_isActive == value)
				{
					return;
				}

				_isActive = value;

				// Get bone content nodes belonging to this armature content node
				IEnumerable<BoneContentNodeViewModel> boneNodes =
					SelfAndDescendants().OfType<BoneContentNodeViewModel>().Where(
						b => b.Armature == this);

				if (_isActive)
				{
					foreach (BoneContentNodeViewModel boneNode in boneNodes)
					{
						boneNode.AxisOverlayState = OverlayState.Normal;
						boneNode.BoneOverlayState = OverlayState.Normal;
					}
				}
				else
				{
					foreach (BoneContentNodeViewModel boneNode in boneNodes)
					{
						boneNode.AxisOverlayState = OverlayState.Hidden;
						boneNode.BoneOverlayState = OverlayState.Hidden;
					}
				}
			}
		}

		public override string SelfAsString
		{
			get
			{
				return Armature.ToString();
			}
		}
		#endregion

		#region Protected Methods
		protected override void SetupSceneAliases(Queue<TransformableObject> axisSceneAliases,
			Queue<TransformableObject> boneSceneAliases)
		{
			// NO-OP
			//
			// Armature content node should not dequeue any aliases because it is not a
			// transformable object
		}
		#endregion
	}
}
